package com.ruicar.afs.cloud.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.admin.service.SysDictDataService;
import com.ruicar.afs.cloud.admin.service.SysDictService;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.admin.entity.SysDict;
import com.ruicar.afs.cloud.admin.entity.SysDictData;
import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * The type DictController.
 * <p>
 * Created on 2019-02-20
 *
 * @author Fzero
 */
@Slf4j
@RestController
@Api(description = "字典管理接口")
@RequestMapping("/dict")
@AllArgsConstructor
public class DictController {

	/**
	 * The Dict service.
	 */
	private SysDictService dictService;

	/**
	 * The Dict data service.
	 */
	private SysDictDataService dictDataService;

	/**
	 * Get all IResponse.
	 *
	 * @return the IResponse
	 */
	@RequestMapping(value = "/getAll", method = RequestMethod.GET)
	@ApiOperation(value = "获取全部数据")
	public IResponse<List<SysDict>> getAll() {
		List<SysDict> list = dictService.list(Wrappers
				.<SysDict>query().lambda()
				.orderByAsc(SysDict::getSortOrder));
		return new IResponse<List<SysDict>>().setData(list);
	}

	/**
	 * Add IResponse.
	 *
	 * @param dict the dict
	 * @return the IResponse
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ApiOperation(value = "添加")
	@Transactional
	@SysLog(value = "添加数据字典类型")
	@PreAuthorize("@pms.hasPermission('sys_dic_add')")
	public IResponse add(@RequestBody SysDict dict) {

		if (dictService.list(Wrappers.<SysDict>query().lambda().eq(SysDict::getType, dict.getType())).size() > 0) {
			return new IResponse<>().setCode(CommonConstants.FAIL).setMsg("字典类型Type已存在");
		}
		dictService.save(dict);
		return new IResponse<>().setMsg("添加成功");
	}

	/**
	 * Edit IResponse.
	 *
	 * @param dict the dict
	 * @return the IResponse
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ApiOperation(value = "编辑")
	@Transactional
	@SysLog(value = "编辑数据字典类型")
	@PreAuthorize("@pms.hasPermission('sys_dic_edit')")
	public IResponse<Object> edit(@RequestBody SysDict dict) {

		SysDict old = dictService.getById(dict.getId());
		// 若type修改判断唯一
		if (!old.getType().equals(dict.getType()) && (dictService.list(Wrappers.<SysDict>query().lambda().eq(SysDict::getType, dict.getType())).size() > 0)) {
			return new IResponse<>().setCode(CommonConstants.FAIL).setMsg("字典类型Type已存在");
		}
		dictService.updateById(dict);
		return new IResponse<Object>().setMsg("编辑成功");
	}

	/**
	 * Del all by ids IResponse.
	 *
	 * @param id the id
	 * @return the IResponse
	 */
	@RequestMapping(value = "/delByIds/{id}", method = RequestMethod.DELETE)
	@ApiOperation(value = "通过id删除")
	@Transactional
	@SysLog(value = "通过id删除")
	@PreAuthorize("@pms.hasPermission('sys_dic_delete')")
	public IResponse<Object> delAllByIds(@PathVariable String id) {


		SysDict dict = dictService.getById(id);
		dictService.removeById(id);
		dictDataService.remove(Wrappers.<SysDictData>query().lambda().eq(SysDictData::getDictId, id));
		// 删除缓存
		return new IResponse<Object>().setMsg("删除成功");
	}

	/**
	 * Search dict list IResponse.
	 *
	 * @param key the key
	 * @return the IResponse
	 */
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	@ApiOperation(value = "搜索字典类型")
	public IResponse<List<SysDict>> searchDictList(@RequestParam String key) {
		List<SysDict> list = dictService.list(Wrappers.<SysDict>query().lambda().like(SysDict::getTitle, key).or().like(SysDict::getType, key));
		return new IResponse<List<SysDict>>().setData(list);
	}
}
